﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Castle.DynamicProxy;
using CNKI.TPI.Web.Base;
using CNKI.TPI.Web.Search.IBLL;
using CNKI.TPI.Web.Search.IDAO;
using CNKI.TPI.Web.Search.Model;
using System.Data;
using System.Collections;
using CNKI.TPI.Web.Admin.IDAO;

namespace CNKI.TPI.Web.Search.BLL
{
    public class NaviService : BaseService, INaviService
    {
        ICategoryDAO categoryDAO = null;
        private IMultiDBPublishDAO multiDbDao = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="categoryDAO"></param>
        /// <param name="userDBDAO"></param>
        public NaviService(ICategoryDAO categoryDAO, IMultiDBPublishDAO multiDbDao)
        {
            this.categoryDAO = this.generator.CreateInterfaceProxyWithTarget<ICategoryDAO>(categoryDAO, new IInterceptor[] { new LogInterceptor() });
            this.multiDbDao = this.generator.CreateInterfaceProxyWithTarget<IMultiDBPublishDAO>(multiDbDao, new IInterceptor[] { new LogInterceptor() });
        }

        /// <summary>
        /// 获得导航分类列表
        /// </summary>
        /// <param name="DBName"></param>
        /// <returns></returns>
        public IList<CLS> GetCategoryType(string DBName, int DBID)
        {
            IList<CLS> result = null;
            SYS_CMS_DATABASE dbinfo = categoryDAO.GetDatabaseByID(DBID);
            if (dbinfo != null)
            {
                if (dbinfo.SearchType == (int)SearchType.TPI)
                {
                    result = categoryDAO.GetCategroyType(DBName, DBID);
                }
                else
                {
                    result = new List<CLS>();
                }
            }
            return result;
        }

        /// <summary>
        /// 获得导航明细
        /// </summary>
        /// <param name="DBName"></param>
        /// <param name="categoryType"></param>
        /// <returns></returns>
        public IList<CLS_NODE> GetCategoryList(string DBName, int categoryType, int DBID)
        {
            return categoryDAO.GetCategroyList(DBName, categoryType, DBID);
        }

        /// <summary>
        /// 获得导航明细
        /// </summary>
        /// <param name="DBName"></param>
        /// <param name="categoryType"></param>
        /// <returns></returns>
        public IList<CLS_NODE> GetCategoryAllList(string DBName, int categoryType, int DBID)
        {
            return categoryDAO.GetCategroyAllList(DBName, categoryType, DBID);
        }

        /// <summary>
        /// 获得导航明细
        /// </summary>
        /// <param name="DBName"></param>
        /// <param name="categoryType"></param>
        /// <returns></returns>
        public IList<CLS_NODE> GetChildrenCategoryList(string DBName, int categoryType, string code, int DBID)
        {
            CLS_NODE node = categoryDAO.GetCategroy(DBName, categoryType, code);
            return categoryDAO.GetChildrenCategroyList(DBName, categoryType, code, node.Grade, DBID);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IList<SYS_CMS_DATABASE_CLS_NODE> GetDataBaseList(PermissionOption option = PermissionOption.FilteringPermissions)
        {
            IList<SYS_CMS_DATABASE_CLS_NODE> result = new List<SYS_CMS_DATABASE_CLS_NODE>();
            try
            {
                //或者所有发布数据库列表
                int videoLibID = int.Parse(ConfigHelper.GetAppSettingsValue("VideoLibID"));
                int picLibID = int.Parse(ConfigHelper.GetAppSettingsValue("PicLibID"));
                int audioLibID = int.Parse(ConfigHelper.GetAppSettingsValue("AudioLibID"));
                IList<SYS_CMS_DATABASE> dbList = multiDbDao.GetPublishDBList(option);
                //dbList = dbList.Where(c => (c.TemplateID != videoLibID && c.TemplateID != picLibID && c.TemplateID != audioLibID)).ToList();

                IList<SYS_CMS_DATABASE_CLS> databaseCLS = categoryDAO.GetDatabaseCLSType();
                if (databaseCLS != null && databaseCLS.Count > 0)
                {
                    IList<SYS_CMS_DATABASE_CLS_NODE> databaseCLSNode = categoryDAO.GetDatabaseCLSNodeType(databaseCLS[0].SysID);
                    if (databaseCLSNode == null || databaseCLSNode.Count == 0)
                    {
                        return null;
                    }

                    IList<SYS_CMS_DATABASE_CLS_DB> databaseCLSDb = categoryDAO.GetDatabaseCLSDBType(databaseCLS[0].SysID, databaseCLSNode.Select(p => p.Code));
                    if (databaseCLSDb == null || databaseCLSDb.Count == 0)
                    {
                        return null;
                    }

                    IList<SYS_CMS_DATABASE> database = categoryDAO.GetDatabaseByCode(databaseCLSDb.Select(p => p.DatabaseCode), option:option);
                    if (database == null || database.Count == 0)
                    {
                        return null;
                    }

                    foreach (SYS_CMS_DATABASE item in database)
                    {
                        IEnumerable<SYS_CMS_DATABASE_CLS_DB> temp = databaseCLSDb.Where(p => p.DatabaseCode == item.DatabaseCode);
                        foreach (SYS_CMS_DATABASE_CLS_DB item1 in temp)
                        {
                            IEnumerable<SYS_CMS_DATABASE_CLS_NODE> item2 = databaseCLSNode.Where(p => p.Code == item1.ClsCode);
                            foreach (SYS_CMS_DATABASE_CLS_NODE item3 in item2)
                            {
                                if (!item3.DatabaseList.Contains(item))
                                {
                                    item3.DatabaseList.Add(item);
                                    RemoveDB(dbList, item.SysID);
                                }
                            }
                        }
                    }
                    foreach (SYS_CMS_DATABASE_CLS_NODE item in databaseCLSNode.Where(p => p.DatabaseList.Count > 0))
                    {
                        result.Add(item);
                    }
                    if (dbList != null && dbList.Count > 0)
                    {
                        SYS_CMS_DATABASE_CLS_NODE node = new SYS_CMS_DATABASE_CLS_NODE();
                        node.Name = "其他";
                        node.DatabaseList = dbList;
                        result.Add(node);
                    }
                }
                else
                {
                    if (dbList != null && dbList.Count > 0)
                    {
                        result.Add(new SYS_CMS_DATABASE_CLS_NODE());
                        result[0].Name = "全部";
                        result[0].DatabaseList = dbList;
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }

        private void RemoveDB(IList<SYS_CMS_DATABASE> dbList, int dbid)
        {
            for (int i = 0; i < dbList.Count; i++)
            {
                if (dbList[i].SysID == dbid)
                {
                    dbList.RemoveAt(i);
                    break;
                }
            }
        }

        public IList<SYS_CMS_PUBLISHTYPE> GetMediaResourceList()
        {
            IList<SYS_CMS_PUBLISHTYPE> result = new List<SYS_CMS_PUBLISHTYPE>();
            try
            {
                string videoLibID = ConfigHelper.GetAppSettingsValue("VideoLibID");
                string picLibID = ConfigHelper.GetAppSettingsValue("PicLibID");
                string audioLibID = ConfigHelper.GetAppSettingsValue("AudioLibID");
                result.Add(new SYS_CMS_PUBLISHTYPE("视频库"));
                result.Add(new SYS_CMS_PUBLISHTYPE("图片库"));
                result.Add(new SYS_CMS_PUBLISHTYPE("音频库"));
                IList<string> templateID = new List<string>();
                templateID.Add(videoLibID);
                templateID.Add(picLibID);
                templateID.Add(audioLibID);
                IList<SYS_CMS_DATABASE> databaseList = categoryDAO.GetDatabaseByTemplateID(templateID);
                foreach (SYS_CMS_DATABASE item in databaseList)
                {
                    if (videoLibID.Equals(item.TemplateID.ToString()))
                    {
                        result[0].DatabaseList.Add(item);
                    }
                    else if (picLibID.Equals(item.TemplateID.ToString()))
                    {
                        result[1].DatabaseList.Add(item);
                    }
                    else if (audioLibID.Equals(item.TemplateID.ToString()))
                    {
                        result[2].DatabaseList.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
        /// <summary>
        /// 删除导航分类
        /// </summary>
        /// <param name="sysid"></param>
        /// <param name="dbCode"></param>
        /// <returns></returns>
        public bool DelCLS(int sysid, string dbCode)
        {
            return categoryDAO.DelCLS(sysid, dbCode);
        }
        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="sysid"></param>
        /// <param name="dbCode"></param>
        /// <returns></returns>
        public bool DelCLS_Node(int sysid, string dbCode)
        {
            return categoryDAO.DelCLS_Node(sysid, dbCode);
        }

        /// <summary>
        /// 插入导航分类列表
        /// </summary>
        /// <param name="DBName"></param>
        /// <returns></returns>
        public bool InsertCategoryType(string dbCode, IList<CLS> categoryTypeList)
        {
            return categoryDAO.InsertCategroyType(dbCode, categoryTypeList);
        }

        /// <summary>
        /// 插入导航分类列表
        /// </summary>
        /// <param name="DBName"></param>
        /// <returns></returns>
        public bool InsertCategoryNode(string dbCode, IList<CLS_NODE> categoryNodeList)
        {
            return categoryDAO.InsertCategroyNode(dbCode, categoryNodeList);
        }

        /// <summary>
        /// 插入导航分类列表
        /// </summary>
        /// <param name="DBName"></param>
        /// <returns></returns>
        public bool UpdateCategoryType(string dbCode, string sysid, Dictionary<string, object> categoryTypeList)
        {
            return categoryDAO.UpdateCategroyType(dbCode, sysid, categoryTypeList);
        }

        /// <summary>
        /// 插入导航分类列表
        /// </summary>
        /// <param name="DBName"></param>
        /// <returns></returns>
        public bool UpdateCategoryNode(string dbCode, string sysid, Dictionary<string, object> categoryNodeList)
        {
            return categoryDAO.UpdateCategroyNode(dbCode, sysid, categoryNodeList);
        }

        /// <summary>
        /// 根据数据库代码、分类字段、分类代码值获得分类名称---LZG添加
        /// </summary>
        /// <param name="DbCode"></param>
        /// <param name="NaviName"></param>
        /// <param name="NaviValue"></param>
        /// <returns></returns>
        public string GetClsNaviName(string DbCode, string NaviName, string NaviValue)
        {
            string NaviText = "";
            CLS clsInfo = categoryDAO.GetCLSInfo(DbCode, NaviName);
            string NaviSysid = clsInfo.SysID.ToString();
            if (NaviSysid != "0")
            {
                string[] ArrNaviValue = NaviValue.Split(new char[] { ',', '，' }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < ArrNaviValue.Length; i++)
                {
                    if (NaviText.Length > 0)
                        NaviText += ",";
                    CLS_NODE clsNodeInfo = categoryDAO.GetCategroy(DbCode, int.Parse(NaviSysid), ArrNaviValue[i]);
                    if (clsNodeInfo != null)
                    {
                        NaviText += clsNodeInfo.Name;
                    }
                }
            }
            return NaviText;
        }

        /// <summary>
        /// 根据数据库代码、分类字段、分类代码值获得分类名称---LZG添加
        /// </summary>
        /// <param name="DbCode"></param>
        /// <param name="NaviName"></param>
        /// <param name="NaviValue"></param>
        /// <returns></returns>
        public int GetClsID(string DbCode, string NaviName)
        {
            int result = 0;
            CLS clsInfo = categoryDAO.GetCLSInfo(DbCode, NaviName);
            if (clsInfo != null)
            {
                result = clsInfo.SysID;
            }
            return result;
        }
        /// <summary>
        /// 根据code 获取数据库实例
        /// </summary>
        /// <param name="dbCode"></param>
        /// <param name="flage">1,过滤权限 0不过滤任何权限直接查询</param>
        /// <returns></returns>
        public SYS_CMS_DATABASE GetDatabase(string dbCode,int flage=1)
        {
            SYS_CMS_DATABASE result = null;
            try
            {
                IList<SYS_CMS_DATABASE> dbList = categoryDAO.GetDatabaseByCode(new List<string> { dbCode },flage);
                if (dbList != null && dbList.Count > 0)
                {
                    result = dbList[0];
                }
            }
            catch (Exception ex)
            {
                LogOpr.Error(ex.ToString());
            }
            return result;
        }
    }
}
